package com.yqb.study.modular.controller.data;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yqb.study.annotation.log.OperateLogAnt;
import com.yqb.study.common.controller.BaseController;
import com.yqb.study.common.from.LoginFrom;
import com.yqb.study.common.session.IRedisSession;
import com.yqb.study.common.webscoket.WebSocket;
import com.yqb.study.constant.CodeMsgEnum;
import com.yqb.study.constant.log.LogTypeEnum;
import com.yqb.study.constant.swagger.SwaggerParamType;
import com.yqb.study.modular.service.IUserService;
import com.yqb.study.modular.warpper.log.AuthLogWarpperMap;
import com.yqb.study.util.ResultVoUtil;
import com.yqb.study.util.ToolUtil;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

// @RestController方法统一返回json报文
@RestController
@Slf4j
@RequestMapping("/data")
public class AuthController extends BaseController {
	@Autowired
	private IUserService userService;
	@Autowired
	private WebSocket webSocket;

	// get测试表单参数校验,全局异常处理,分布式session	
	@OperateLogAnt(logTypeEnum = LogTypeEnum.LOGIN_LOG, logName = "登录验证账户密码", logWapperClazz = AuthLogWarpperMap.class)
	@GetMapping(value = "/loginAuthGet")
	public Object helloLoginGet(@Valid LoginFrom loginFrom, HttpServletRequest request,
			HttpServletResponse response, IRedisSession sessionRedis) {

		String token = userService.userLogin(loginFrom, request, response, sessionRedis);

		return ResultVoUtil.success(CodeMsgEnum.LOGIN_SUCCESS, token);
	}	
	
	@OperateLogAnt(logTypeEnum = LogTypeEnum.LOGOUT_LOG, logName = "用户注销登录", logWapperClazz = AuthLogWarpperMap.class)
	@GetMapping("/logoutAuthGet")
	public Object helloLogoutGet(HttpServletRequest request, HttpServletResponse response, IRedisSession sessionRedis) {

		// 断开websocket连接
		if (ToolUtil.isNotEmpty(sessionRedis.getSessionRedis())
				&& ToolUtil.isNotEmpty(sessionRedis.getSessionRedis().getUser())) {
			String phone = sessionRedis.getSessionRedis().getUser().getPhone();
			String token = sessionRedis.getSessionRedis().getToken();
			webSocket.closeConnect(phone, token);
		}

		// 删除cookie和session
		userService.userLogout(request, response, sessionRedis);

		return ResultVoUtil.success(CodeMsgEnum.LOGOUT_SUCCESS);
	}	
	
	// post测试表单参数校验,全局异常处理,分布式session
//	@ApiOperation("用户登录后台参数校验")
//	@OperateLogAnt(logTypeEnum = LogTypeEnum.LOGIN_LOG, logName = "登录验证账户密码", logWapperClazz = AuthLogWarpperMap.class)
//	@PostMapping(value = "/loginAuthPost")
//	public Object helloLoginPost(@Valid @RequestBody LoginFrom loginFrom, HttpServletRequest request,
//			HttpServletResponse response, IRedisSession sessionRedis) {
//
//		String token = userService.userLogin(loginFrom, request, response, sessionRedis);
//
//		return ResultVoUtil.success(CodeMsgEnum.LOGIN_SUCCESS, token);
//	}
//
//	// 测试分布式session
//	@ApiOperation(value = "用户登录注销", notes = "删除交互cookie和session")
//	@ApiImplicitParams({
//			@ApiImplicitParam(paramType = SwaggerParamType.HEADER, name = "token", value = "浏览器token", required = true, dataType = "String", defaultValue = "无") })
//	@OperateLogAnt(logTypeEnum = LogTypeEnum.LOGOUT_LOG, logName = "用户注销登录", logWapperClazz = AuthLogWarpperMap.class)
//	@PostMapping("/logoutAuthPost")
//	public Object helloLogoutPost(HttpServletRequest request, HttpServletResponse response, IRedisSession sessionRedis) {
//
//		// 断开websocket连接
//		if (ToolUtil.isNotEmpty(sessionRedis.getSessionRedis())
//				&& ToolUtil.isNotEmpty(sessionRedis.getSessionRedis().getUser())) {
//			String phone = sessionRedis.getSessionRedis().getUser().getPhone();
//			String token = sessionRedis.getSessionRedis().getToken();
//			webSocket.closeConnect(phone, token);
//		}
//
//		// 删除cookie和session
//		userService.userLogout(request, response, sessionRedis);
//
//		return ResultVoUtil.success(CodeMsgEnum.LOGOUT_SUCCESS);
//	}
	
	
	// post测试表单参数校验,全局异常处理,分布式session
	@ApiOperation("登录验证账户密码")
	@OperateLogAnt(logTypeEnum = LogTypeEnum.LOGIN_LOG, logName = "登录验证账户密码", logWapperClazz = AuthLogWarpperMap.class)
	@PostMapping(value = "/loginAuthPost")
	public Object helloLoginPost(@Valid @RequestBody LoginFrom loginFrom, HttpServletRequest request,
			HttpServletResponse response, IRedisSession sessionRedis) {

		String token = userService.userLogin(loginFrom, request, response, sessionRedis);

		return ResultVoUtil.success(CodeMsgEnum.LOGIN_SUCCESS, token);
	}

	// 测试分布式session
	@OperateLogAnt(logTypeEnum = LogTypeEnum.LOGOUT_LOG, logName = "用户注销登录", logWapperClazz = AuthLogWarpperMap.class)
	@PostMapping("/logoutAuthPost")
	public Object helloLogoutPost(HttpServletRequest request, HttpServletResponse response, IRedisSession sessionRedis) {

		// 断开websocket连接
		if (ToolUtil.isNotEmpty(sessionRedis.getSessionRedis())
				&& ToolUtil.isNotEmpty(sessionRedis.getSessionRedis().getUser())) {
			String phone = sessionRedis.getSessionRedis().getUser().getPhone();
			String token = sessionRedis.getSessionRedis().getToken();
			webSocket.closeConnect(phone, token);
		}

		// 删除cookie和session
		userService.userLogout(request, response, sessionRedis);

		return ResultVoUtil.success(CodeMsgEnum.LOGOUT_SUCCESS);
	}	
}
